package com.xxx.video.commen.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xxx.video.R;
import com.xxx.video.commen.utils.StringUtils;

public class ExpandableTextView extends LinearLayout {
    private TextView mContentTextView;
    private TextView mExpansionButton;

    private int mMaxLine = 3;
    private CharSequence mContent;

    private boolean mIsExpansion;

    public ExpandableTextView(Context context) {
        super(context);
        init(context, null);
    }

    public ExpandableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ExpandableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        setOrientation(VERTICAL);
        LayoutInflater.from(context).inflate(R.layout.diy_v_expendable_text, this);

        mContentTextView = findViewById(R.id.tv_diy_v_expendable_text_content);
        mExpansionButton = findViewById(R.id.tv_diy_v_expendable_text_button);

        mContentTextView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        if (mContentTextView.getWidth() == 0) {
                            return;
                        }
                        mContentTextView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        setText(mContent);
                    }
                }
        );

        mExpansionButton.setOnClickListener(v -> toggleExpansionStatus());
        mExpansionButton.setVisibility(GONE);
    }

    /**
     *
     */
    private void toggleExpansionStatus() {
        mIsExpansion = !mIsExpansion;

        if (mIsExpansion) {
            mExpansionButton.setText(StringUtils.getInstance().getString(R.string.tv_diy_v_expendable_text_button_pack_up));
            mContentTextView.setMaxLines(Integer.MAX_VALUE);
        } else {
            mExpansionButton.setText(StringUtils.getInstance().getString(R.string.tv_diy_v_expendable_text_button_expend));
            mContentTextView.setMaxLines(mMaxLine);
        }
    }

    /**
     *
     */
    public void setMaxLine(int maxLine) {
        this.mMaxLine = maxLine;
        setText(mContent);
    }

    /**
     *
     */
    public void setText(CharSequence text) {
        mContent = text;

        if (mContentTextView.getWidth() == 0) {
            return;
        }

        mContentTextView.setMaxLines(Integer.MAX_VALUE);
        mContentTextView.setText(mContent);

        int lineCount = mContentTextView.getLineCount();

        if (lineCount > mMaxLine) {
            mExpansionButton.setVisibility(VISIBLE);
            mExpansionButton.setText(StringUtils.getInstance().getString(R.string.tv_diy_v_expendable_text_button_pack_up));
            mContentTextView.setMaxLines(mMaxLine);
            mIsExpansion = false;

        } else {
            mExpansionButton.setVisibility(GONE);
        }
    }

}
